﻿using System;
using System.Drawing.Drawing2D;
using System.Drawing;
using System.Collections;

namespace TestAnimation
{
    class ScreenObject
    {
        public Vector position;
        protected Vector velocity = new Vector(Vector.Zero);
        protected Pen pen = new Pen(Color.White);
        protected ArrayList points;
        protected ArrayList transformedPoints;
        protected double speed;
        protected double courseOrientation;
        protected double sinObject, cosObject;    //precalculated
        protected int objectWidth;
        protected int objectHeight;
        protected double objectOrientation;
        protected int rotationSpeed;
        protected int objectSize;


        public ArrayList TransformedPoints
        {
            get { return transformedPoints; }
        }
        public bool Collide(Point p)
        {
            Vector v = position - new Vector(p.X, p.Y);
            if(v.Magnitude()>objectSize * 1.5) return (false); //fast test

            var myPath = new GraphicsPath();
            var transPoints = new Point[transformedPoints.Count];
            int i=0;
            foreach (Point point in transformedPoints)
            {
                transPoints[i]=new Point(point.X,point.Y);
                i++;
            }
            myPath.AddPolygon(transPoints);
            myPath.CloseFigure();
            return myPath.IsVisible(p);     //exact (and slow) test
        }

        public void TransformCoordinates()
        {
            Vector pos;      
            transformedPoints.Clear();
            foreach (Point p in points)
            {
                pos = new Vector(p.X, p.Y);
                pos = pos.Rotate((float)sinObject, (float)cosObject) + position;    //rotacion & translation
                transformedPoints.Add(new Point((int) pos.X,(int) pos.Y));
            }
        }


        public void Draw(Screen screen)
        {
            screen.Add(transformedPoints);
        }


        public virtual void Move()
        {
            position += velocity;
            if (position.X > Globals.maxX + objectSize / 2) position.X = -objectSize / 2f;
            if (position.Y > Globals.maxY + objectSize / 2) position.Y = -objectSize / 2f;
            if (position.X < -objectSize / 2f) position.X = Globals.maxX + objectSize / 2;
            if (position.Y < -objectSize / 2f) position.Y = Globals.maxY + objectSize / 2;
        }

        public Vector Position
        {
            get { return position; }
        }
        public int Size
        {
            get { return objectSize; }
        }
        public Vector Velocity
        {
            get { return velocity; }
        }

        protected void SetVelocity()
        {
            velocity = new Vector((float)speed, 0).Rotate((float)courseOrientation);
        }

        public double ObjectOrientation
        {
            get { return objectOrientation / Math.PI * 180; }    //convert into degrees
            set
            {
                if (value > 360) value -= 360;
                objectOrientation = value * Math.PI / 180;       //convert into radians
                sinObject = Math.Sin(objectOrientation);         //cache sinus and cosinus...
                cosObject = Math.Cos(objectOrientation);         //for mouvement orientation
            }
        }
        public double CourseOrientation
        {
            get { return courseOrientation / Math.PI * 180; }    //convert into degrees
            set
            {
                if (value > 360) value -= 360;
                courseOrientation = value * Math.PI / 180;          //convert into radians
            }
        }
        public double Speed
        {
            get { return speed; }
            set
            {
                speed = Math.Min(value, Globals.maxSpeed);
                if (value > Globals.maxSpeed) speed = Globals.maxSpeed;
                SetVelocity();
            }
        }

    }
}
